package com.shihan.config;

import com.shihan.pojo.Configuration;
import com.shihan.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class XMLMapperParse {

    private Configuration configuration = null;

    public XMLMapperParse(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream is) throws DocumentException {
        Document document = new SAXReader().read(is);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        // 因为在底层使用 JDBC 进行操作时，查询用的是 executeQuery，而 增、删、改 用的是 executeUpdate
        // 所以为了方便起见，给 MappedStatement 一个标志位，区别是 查询 还是其他 增、删、改 操作

        List<Element> nodes = rootElement.selectNodes("//select");
        if(nodes!=null&&nodes.size()>0){
            for(Element element:nodes){
                String id = element.attributeValue("id");
                String resultType = element.attributeValue("resultType");
                String paramType = element.attributeValue("paramType");
                String sqlText = element.getTextTrim();

                MappedStatement mappedStatement = new MappedStatement();
                mappedStatement.setQuery(true);
                mappedStatement.setId(id);
                mappedStatement.setParameterType(paramType);
                mappedStatement.setResultType(resultType);
                mappedStatement.setSql(sqlText);

                String key = namespace + "." + id;
                configuration.getMappedStatementMap().put(key,mappedStatement);
            }
        }

        // 解析 insert、update、delete 标签， 统一保存 增、删、改 标签信息到集合中；
        List<Element> notSelect = new ArrayList<>();

        List<Element> insertNodes = rootElement.selectNodes("//insert");
        List<Element> updateNodes = rootElement.selectNodes("//update");
        List<Element> deleteNodes = rootElement.selectNodes("//delete");

        if(insertNodes!=null&&insertNodes.size()>0){
            notSelect.addAll(insertNodes);
        }
        if(updateNodes!=null&&updateNodes.size()>0){
            notSelect.addAll(updateNodes);
        }
        if(deleteNodes!=null&&deleteNodes.size()>0){
            notSelect.addAll(deleteNodes);
        }


        if(notSelect.size()>0){
            for(Element element:notSelect){
                String id = element.attributeValue("id");
                String resultType = element.attributeValue("resultType");
                String paramType = element.attributeValue("paramType");
                String textTrim = element.getTextTrim();

                MappedStatement mappedStatement = new MappedStatement();
                mappedStatement.setQuery(false);
                mappedStatement.setId(id);
                mappedStatement.setResultType(resultType);
                mappedStatement.setParameterType(paramType);
                mappedStatement.setSql(textTrim);

                String key = namespace + "." + id;
                configuration.getMappedStatementMap().put(key,mappedStatement);
            }
        }

    }
}
